package com.witframework.security;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class EncryptFile {
  private final static byte[] KEY=new byte[] { 100, 125, 28, -122, -26, 94, -3, -72 };
  public static void encrypt(byte[] keyBytes,String textPath,String keypath,String cipherPath) throws Exception {	  	
	  	KeyGenerator kg = KeyGenerator.getInstance("DES");
	    kg.init(new SecureRandom());
	    SecretKey key = kg.generateKey();
	    key=new SecretKeySpec(keyBytes, "DES");
	    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
	    Class spec = Class.forName("javax.crypto.spec.DESKeySpec");
	    DESKeySpec ks = (DESKeySpec) skf.getKeySpec(key, spec);    
	    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(keypath));
	    oos.writeObject(ks.getKey());

	    Cipher c = Cipher.getInstance("DES/CFB8/NoPadding");
	    c.init(Cipher.ENCRYPT_MODE, key);
	    CipherOutputStream cos = new CipherOutputStream(new FileOutputStream(cipherPath), c);
	    
	    InputStream in=new FileInputStream(textPath);
		byte[] contents = new byte[10240];
		int readSize=0;
		while((readSize=in.read(contents))>0){
			cos.write(contents,0,readSize);
		}		
		cos.flush();
	    cos.close();
	    oos.writeObject(c.getIV());
	    oos.close();
  }
  public static void encrypt(String textPath,String keypath,String cipherPath) throws Exception {
	  encrypt(KEY,textPath,keypath,cipherPath);
  }
  public static void encrypt(String keyString,String textPath,String keypath,String cipherPath) throws Exception {
	  keyString=keyString+"wuxinyang";
	  keyString=keyString.substring(0,8);
	  encrypt(keyString.getBytes(),textPath,keypath,cipherPath);
  }
  public static void encrypt(String keyString,String textPath) throws Exception {	 
	  encrypt(keyString,textPath,new File(textPath).getParent()+File.separator+"keyFile",textPath+".enc");
  }
  public static void encryptFile(String keyString,String textPath) throws Exception {	 
	  new File(textPath).renameTo(new File(textPath+".plain"));
	  encrypt(keyString,textPath+".plain",new File(textPath+".plain").getParent()+File.separator+"keyFile",textPath);
  }
  public static void encrypt(String textPath) throws Exception {	  
	  encrypt(KEY,textPath,new File(textPath).getParent()+File.separator+"keyFile",textPath+".enc");
  }
  public static void encryptFile(String textPath) throws Exception {	  
	  new File(textPath).renameTo(new File(textPath+".plain"));
	  encrypt(KEY,textPath+".plain",new File(textPath+".plain").getParent()+File.separator+"keyFile",textPath);
  }
  public static void main(String args[]) throws Exception {
	  if(args.length==0){
		  System.out.println("param error!");
	  }
	  if(args.length==4){
		  encrypt(args[0],args[1],args[2],args[3]);
	  }
	  if(args.length==2){
		  encryptFile(args[0],args[1]);
	  }
	  if(args.length==1){
		  encryptFile(args[0]);
	  }
  }
}
